package com.summer.shopping.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.shopping.domain.table.Product;
import com.summer.shopping.domain.table.SecondaryCategory;
import com.summer.shopping.domain.table.dto.SecondaryDTO;
import com.summer.shopping.mapper.ProductMapper;
import com.summer.shopping.mapper.SecondaryCgMapper;
import com.summer.shopping.service.SecondaryCgService;
import com.summer.shopping.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * ClassName:
 * Package:
 * Description:
 *
 * @Author: 陈三岁
 * @Create
 * @Version
 */
@Service
@Slf4j
public class SecondaryCgServiceImpl extends ServiceImpl<SecondaryCgMapper, SecondaryCategory>
        implements SecondaryCgService {

    @Autowired
    private SecondaryCgMapper secondaryCgMapper;


    /**
     * 二级分类名称查询
     * @param id
     * @return
     */
    public List<SecondaryCategory> selectByCgId(Long parentId) {
            return secondaryCgMapper.selectAllSCg(parentId);
    }

    /**
     * 新增二级分类
     * @param secondaryDTO
     */
    public void saveSCg(SecondaryDTO secondaryDTO) {

        secondaryCgMapper.save(secondaryDTO);
    }

    /**
     * 根据二级分类id删除二级分类
     * @param id
     */
    public Result deleteById(Long id) {
        List<Product> productList = new ArrayList<>();


        //如果if结果为true，就删除关联商品
        if (productList != null && productList.size() > 0){
            return Result.error("请删除该类下的所有商品");
        }

        secondaryCgMapper.deleteById(id);

        return Result.ok("删除成功");
    }
}
